package com.luoke.util;

import cn.hutool.core.util.ReUtil;
import com.luoke.context.GlobalDataStore;
import com.luoke.pojo.dto.BattleInfoDTO;
import com.luoke.pojo.entity.Attribute;

import static com.luoke.constant.Attribute.DARK;

/**
 * BattleUtils 工具类
 * 提供与战斗相关的常用方法。
 */
public class BattleUtils {

    /**
     * 获取对手玩家的名字
     *
     * @param playerName 当前玩家的名字
     * @return 对手玩家的名字，如果未找到则返回 null
     */
    public static String getOpponentPlayerName(String playerName) {
        // 获取当前玩家的 BattleInfoDTO
        BattleInfoDTO battleInfo = GlobalDataStore.getBattleInfoByPlayerName(playerName);
        if (battleInfo == null) {
            return null; // 当前玩家没有参与任何战斗
        }

        // 判断当前玩家是 player1 还是 player2，并返回对手玩家的名字
        if (playerName.equals(battleInfo.getPlayer1Name())) {
            return battleInfo.getPlayer2Name();
        } else if (playerName.equals(battleInfo.getPlayer2Name())) {
            return battleInfo.getPlayer1Name();
        } else {
            return null; // 玩家名不匹配，不属于这场战斗
        }
    }

    /**
     * 移除全局对战信息
     *
     * @param user1 玩家1
     * @param user2 玩家2
     */
    public static void removeGlobalBattleMessage(String user1, String user2) {
        GlobalDataStore.removePlayerFromBattleInfo(user1);
        GlobalDataStore.removePlayerFromBattleInfo(user1);
        GlobalDataStore.removePlayerFromBattle1Id(user2);
        GlobalDataStore.removePlayerFromBattle1Id(user2);
    }


    /**
     * 计算伤害
     *
     * @param attackerName 攻击方玩家名
     * @param defenderName 防守方玩家名
     * @return 伤害结果描述
     */
    public static String calculateDamage(String attackerName, String defenderName,int petSkillValue) {
        // 从全局数据中获取战斗信息
        BattleInfoDTO attackerBattleInfo = GlobalDataStore.getBattleInfoByPlayerName(attackerName);
        BattleInfoDTO defenderBattleInfo = GlobalDataStore.getBattleInfoByPlayerName(defenderName);

        if (attackerBattleInfo == null || defenderBattleInfo == null) {
            throw new IllegalArgumentException("未找到玩家的战斗信息！");
        }

        // 获取攻击方和防守方的宠物属性
        Attribute attackerAttribute = attackerBattleInfo.getPetAttributeByPlayerName(attackerName);
        Attribute defenderAttribute = defenderBattleInfo.getPetAttributeByPlayerName(defenderName);

        if (attackerAttribute == null || defenderAttribute == null) {
            throw new IllegalArgumentException("未找到玩家的宠物属性！");
        }

        // 获取等级信息
        int attackerLevel = attackerBattleInfo.getPetLevelByPlayerName(attackerName);
        int defenderLevel = defenderBattleInfo.getPetLevelByPlayerName(defenderName);



        // 力量加成
        int powerBonus = attackerAttribute.getPower() != null ? attackerAttribute.getPower() : 0;

        // 等级差倍率
        double levelMultiplier = 1 + (attackerLevel - defenderLevel) * 0.05;
        levelMultiplier = Math.max(0.5, levelMultiplier); // 最低倍率为 0.5

        // 属性克制倍率
        double typeMultiplier = getEffectiveness(attackerAttribute.getMain(), defenderAttribute.getMain());

        // 准确度与敏捷影响
        int dodgeChance = defenderAttribute.getDexterity() - attackerAttribute.getAccuracy();
        dodgeChance = Math.max(0, dodgeChance); // 确保躲避概率不为负

        if (new java.util.Random().nextInt(100) < dodgeChance) {
            return String.format("攻击未命中！%s 的宠物躲避了 %s 的攻击！", defenderName, attackerName);
        }

        // 防御力减免
        int defenceReduction = defenderAttribute.getDefence() != null ? defenderAttribute.getDefence() : 0;

        // 最终伤害计算
        int rawDamage = (int) (petSkillValue * levelMultiplier * typeMultiplier) + powerBonus;
        int finalDamage = Math.max(0, rawDamage - defenceReduction);

        // 构造结果描述
        return String.format(
                "%s 的宠物对 %s 的宠物造成了 %d 点伤害！（基础伤害: %d，力量加成: %d，等级倍率: %.2f，属性克制倍率: %.2f，防御减免: %d）",
                attackerName,
                defenderName,
                finalDamage,
                petSkillValue,
                powerBonus,
                levelMultiplier,
                typeMultiplier,
                defenceReduction
        );
    }

    /**
     * 获取属性克制倍率
     *
     * @param attackerMain 攻击方的属性Main
     * @param defenderMain 防守方的属性Main
     * @return 克制倍率，克制为 2.0，被克制为 0.5，无克制为 1.0
     */
    private static double getEffectiveness(String attackerMain, String defenderMain) {
        if (attackerMain == null || defenderMain == null) {
            return 1.0; // 默认无克制关系
        }

        switch (attackerMain) {
            case "暗":
                if ("光".equals(defenderMain) || "草".equals(defenderMain)) {
                    return 2.0;
                }
                break;
            case "光":
                if ("暗".equals(defenderMain) || "水".equals(defenderMain)){
                    return 2.0;
                }
                break;
            case "水":
                if ("火".equals(defenderMain) || "草".equals(defenderMain)){
                    return 2.0;
                }
                break;
            case "火":
                if ("草".equals(defenderMain) || "暗".equals(defenderMain)){
                    return 2.0;
                }
                break;
            case "草":
                if ("光".equals(defenderMain) || "水".equals(defenderMain)){
                    return 2.0;
                }
                break;
        }

        // 被克制情况
        switch (defenderMain) {
            case "暗":
                if ("光".equals(attackerMain) || "草".equals(attackerMain)){
                    return 0.5;
                }
                break;
            case "光":
                if ("暗".equals(attackerMain) || "水".equals(attackerMain)){
                    return 0.5;
                }
                break;
            case "水":
                if ("火".equals(attackerMain) || "暗".equals(attackerMain)){
                    return 0.5;
                }
                break;
            case "火":
                if ("水".equals(attackerMain) || "光".equals(attackerMain)){
                    return 0.5;
                }
                break;
            case "草":
                if ("火".equals(attackerMain) || "暗".equals(attackerMain)){
                    return 0.5;
                }
                break;
        }

        return 1.0; // 无克制关系
    }



    /**
     * 从伤害描述字符串中提取伤害值
     *
     * @param resultDescription 伤害描述字符串
     * @return 伤害值，如果未找到则返回 -1
     */
    public static int extractDamage(String resultDescription) {
        // 定义正则表达式，匹配“造成了 数字 点伤害”
        String regex = "造成了 (\\d+) 点伤害";

        // 使用 ReUtil 提取第一个匹配的数字
        String damageStr = ReUtil.getGroup1(regex, resultDescription);

        if (damageStr != null) {
            try {
                return Integer.parseInt(damageStr);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }

        // 未找到匹配或转换失败，返回 0 表示扣减0伤害
        return 0;
    }
}





